﻿//获取Token参数
	var Request = new Object();
	Request = GetRequest();
	var token= Request['Token'];
	var deptID = "" ;
	var userID= "" ;
	var deptName = "" ;
	var userType = "" ;
  	$('.mb').hide();
	$('.datagrid-sort-icon').hide();	
$(document).ready(function(){
	var grid=$('#grid').datagrid({
		width:'auto',
		height:'auto',
		fit:true,
		nowrap: false,
		striped: true,
		collapsible : false,
		singleSelect:true,		
		url:'action/master/usermanage/list.json?Token=' + token,
			sortName: 'F_CODE',
			sortOrder: 'asc',
			remoteSort: false,			
			loadMsg:'正在载入数据...',
			idField:'F_ID',
//			fitColumns:true,
			columns:[[
//			          {checkbox:true},
			          {field:'F_CODE',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: center;width: 100%;'>编码</span></div>",width:80,sortable:true,align:'left'},
			          {field:'F_NAME',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>名称</span></div>",width:120,sortable:true,align:'left'},			          
			          {field:'F_PHONENO',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>电话</span></div>",width:120,sortable:true,align:'left'},
			          {field:'DEPTNAME',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>所属单位</span></div>",width:150,sortable:true,align:'left',
			        	  formatter:function(value,rec){
			        	  var str=value;
			        	  if (value==""||value==null)
			        		  str="所有";			        	  
			        	  return str;
			          }},
			          {field:'GROUPNAME',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>用户组</span></div>",width:150,sortable:true,align:'left'},
			          {field:'F_USERTYPE',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>用户类型</span></div>",width:100,sortable:true,align:'left',
			        	  formatter:function(value,rec){
			        	  var str="一般用户";
			        	  if (value=="A")
			        		  str="超级管理员";
			        	  if (value=="G")
			        		  str="管理员";
			        	  return str;
			          }
			          },
		              {field:'F_STATE',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>状态</span></div>",width:80,sortable:true,align:'center',
			        	  formatter:function(value,rec){
			        	  var str="启用";
			        	  if (value=="1")
			        		  str="停用";
			        	  return str;
			          }
			          }
			          ]],
		pagination:true,
		rownumbers:false,
		onBeforeLoad:function(param){
			$('.mb').hide();
			$('.datagrid-sort-icon').hide();

			$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
			$('div.datagrid-view2>div.datagrid-header').attr('id','gheader');
			$('#gheader').css('height','35px');
			$('#gheader>div.datagrid-header-inner').attr('id','gInner');
			$('#gInner>table').attr('id','gTable');
			$('#gTable').css('height','35px');
			$('#gTable').find('td').css('height','30px');
			$('#gTable').find('td').css('line-height','30px');
									},
		onLoadSuccess:function(){
			// 默认选中第一行
			if($(this).datagrid("getRows").length > 0){
//				$(this).datagrid("selectRow", 0);
			} 
			
			$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
			$('div.datagrid-view2>div.datagrid-header').attr('id','gheader');
			$('#gheader').css('height','35px');
			$('#gheader>div.datagrid-header-inner').attr('id','gInner');
			$('#gInner>table').attr('id','gTable');
			$('#gTable').css('height','35px');
			$('#gTable').find('td').css('height','30px');
			$('#gTable').find('td').css('line-height','30px');
		},onDblClickRow:function(rowIndex,rowData){
			update();
		},toolbar:[{
			id:'btnRefresh',
			text:'刷新',
			iconCls:'icon-reload',
			handler:function(){
			  GirdReload();
			}
		 }, '-',{
			id:'btnadd',
			text:'新增',
			iconCls:'icon-add',
			handler:function(){
				$("#addForm").attr("action","action/master/usermanage/add.json?Token=" + token);				
				$.getJSON("action/master/usermanage/getMaxCode.json?Token=" + token+"&rnd="+Math.random(), function(data){
                        $("#addForm input[name=F_CODE]").val(data.F_CODE);
                        $("#addForm input[name=F_PASSWORD]").val("666666");
                        $("#addForm input[name=F_REPASSWORD]").val("666666");
                        $("#addForm input[name=F_STATE]").val("0");
//                        $("#addForm input[name=F_USERTYPE]").val("一般用户");
                        $("#addusertype").combobox('setText',"一般用户");
                        $("#addusertype").combobox('setValue',"P");
                });
				openDialog('新增','addDig','grid','addForm',{'height': 310,'width':300,clearForm:true},
					function(){ 
						$("#addDig").dialog('open'); 
						$.getJSON("action/master/usermanage/getMaxCode.json?Token=" + token+"&rnd="+Math.random(), function(data){
                        $("#addForm input[name=F_CODE]").val(data.F_CODE);
                        $("#addForm input[name=F_PASSWORD]").val("666666");
                        $("#addForm input[name=F_REPASSWORD]").val("666666");
                        $("#addForm input[name=F_STATE]").val("0");
                        $("#addusertype").combobox('setText',"一般用户");
                        $("#addusertype").combobox('setValue',"P");
                });
					}
				);
			}
		},'-',{
			id:'btnEdit',
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				update();
			}
		},'-',{
			id:'btnDel',
			text:'删除',
			iconCls:'icon-no',
			handler:function(){
				$("#deleteForm").attr("action","action/master/usermanage/del.json?Token=" + token);
				var select = $('#grid').datagrid('getSelected');
				if(select)
					var msg = "您确定要删除账户【" + select.F_CODE + " " + select.F_NAME + "】吗?";
				$("#deleteForm input[name=F_ID]").val(select.F_ID);
				if(select.F_STATE=="0")
				{
				    msg = "当前账号已启用，请先停用后再删除!";
//				    $.messager.alert("提示",msg);
				    parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>"+msg+"</p>",timeout:3000,showType:'slide',width:250,height:150});
				}
				else
				{
				    del("grid","delId","deleteForm","F_ID",msg,function(){
				    	$('#grid').datagrid('clearSelections');
				    });
				    
				};				
			}
		},'-',{
			id:'btnQry',
			text:'查询',
			iconCls:'icon-search',
			handler:function(){
			  FindData();
			}
		 },'-',{
			id:'btnEnabled',
			text:'启用',
			iconCls:'icon-redo',
			handler:function(){
			    var select = $('#grid').datagrid('getSelected');
				if(select.F_STATE=="0")
				{
				    msg = "当前账号已处于启用状态,不能重复启用!";				    
				    parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>"+msg+"</p>",timeout:3000,showType:'slide',width:250,height:150});
				    SelGirdRow(select.F_ID);
				}
				else
				{
			    $.getJSON("action/master/usermanage/editState.json?F_ID="+select.F_ID+"&F_STATE=0&Token=" + token+"&rnd="+Math.random(),
                function(data){
				clearSelect('grid');				
				parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>"+data.msg+"</p>",timeout:3000,	showType:'slide',width:250,height:150});
				$('#grid').datagrid('reload');
				SelGirdRow(select.F_ID);
			    });
			    
				}			    
		 }
		 },'-',{
			id:'btnDisEnabled',
			text:'停用',
			iconCls:'icon-undo',
			handler:function(){
			    var select = $('#grid').datagrid('getSelected'); 
				if(select.F_STATE=="1")
				{
				    msg = "当前账号已处于停用状态,不能重复停用!";
				    SelGirdRow(select.F_ID);
				    parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>"+msg+"</p>",timeout:3000,	showType:'slide',width:250,height:150});
				}
				else
				{
				$.getJSON("action/master/usermanage/editState.json?F_ID="+select.F_ID+"&F_STATE=1&Token=" + token+"&rnd="+Math.random(),
                function(data){
				parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>"+data.msg+"</p>",timeout:3000,	showType:'slide',width:250,height:150});
				clearSelect('grid');
				$('#grid').datagrid('reload');
				SelGirdRow(select.F_ID);
				});
				}                
			}
		 }
		]
	});
	
	//用户组--新增
	$('#addUserGroup').combobox({  
    url:'action/master/usergroup/comboxList.json?Token=' + token,  
    valueField:'id',  
    textField:'text',
    panelHeight:100,
	editable:false
    });  
	//用户组--修改
	$('#txtusergroup').combobox({  
    url:'action/master/usergroup/comboxList.json?Token=' + token,  
    valueField:'id',  
    textField:'text',
    panelHeight:100,
	editable:false
    }); 
	//用户类型--新增
	$('#addusertype').combobox({  
    url:'action/master/usermanage/initUserType.json?Token=' + token,  
    valueField:'id',  
    textField:'text',
	editable:false,
	panelHeight:42
    }); 
	//用户类型--修改
	$('#txtusertype').combobox({  
    url:'action/master/usermanage/initUserType.json?Token=' + token,  
    valueField:'id',  
    textField:'text',
	editable:false,
	panelHeight:42
    }); 
	//所属单位--新增
	$('#addDept').combotree({
		url :'action/basic/depart/userTree.json?Token=' + token,
		panelWidth:'300',
		onDblClick: function(node)
		{
			$(this).tree('toggle', node.target);
			if(node==null||node.text=="所有"||node.text=="")
			{
				return false ;
			}
			else
			{
				$('#addDept').val(node.text);	
			}	
		},
		onClick: function(node)
		{
			if(node==null||node.text=="所有"||node.text=="")
			{
				return false ;
			}
			else
			{
				$('#addDept').val(node.text);	
			}			
        },
        onBeforeSelect: function(node)
        {
        	if(node==null||node.text=="所有"||node.text=="")
        	{
        		return false ;
        	}
        },
        onLoadSuccess: function(node,data)
        {
        	var rootnode = $(this).tree('getRoot');
        	$(this).tree('expand', rootnode.target);
        },onShowPanel:function()
		{
			$(this).combotree('panel').parent('.panel').css('border-bottom','#87cedb 3px solid');
			$(this).combotree('panel').parent('.panel').css('border-left','#87cedb 3px solid');
			$(this).combotree('panel').parent('.panel').css('border-right','#87cedb 3px solid');
		}
	});
	//所属单位--修改
	$('#edtDept').combotree({
		url :'action/basic/depart/userTree.json?Token=' + token,
		panelWidth:'300',
		onDblClick: function(node)
		{
			$(this).tree('toggle', node.target);
			DeptID = node.id;
			if(node==null||node.text=="所有"||node.text=="")
			{
				$('#edtDept').val("");
			}
			else
			{
				$('#edtDept').val(node.text);	
			}	
		},onClick: function(node)
		{
			if(node==null||node.text=="所有"||node.text=="")
			{
				$('#edtDept').combotree('setValue',DeptName);
			}
			else
			{
				$('#edtDept').val(node.text);	
				DeptID = node.id ;
			}	
        },onBeforeSelect: function(node)
        {
        	if(node==null||node.text=="所有"||node.text=="")
        	{
        		return false ;
        	}
        },onLoadSuccess: function(node,data)
        {
        	var rootnode = $(this).tree('getRoot');
        	$(this).tree('expand', rootnode.target);
        },onShowPanel:function()
		{
			$(this).combotree('panel').parent('.panel').css('border-bottom','#87cedb 3px solid');
			$(this).combotree('panel').parent('.panel').css('border-left','#87cedb 3px solid');
			$(this).combotree('panel').parent('.panel').css('border-right','#87cedb 3px solid');
		}
	});
	$('#addForm input').focusout(trimkeyup);
	$('#updateForm input').focusout(trimkeyup);
	$('#queryForm input').focusout(trimkeyup);
	
	$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
	$('div.datagrid-view2>div.datagrid-header').attr('id','gheader');
	$('#gheader').css('height','35px');
	$('#gheader>div.datagrid-header-inner').attr('id','gInner');
	$('#gInner>table').attr('id','gTable');
	$('#gTable').css('height','35px');
//	$('#txtcode').keydown(onlyNum);
//	$('#addcode').keydown(onlyNum);
});

//把信息放入回复留言页面
function inputdata(data)
{
	$("#txtuser").val(data.F_USER);
	$("#txtpwd").val(data.F_PWD);
	$("#txtmemo").val(data.F_MEMO);
	$("#txtid").val(data.F_ID);
	if (data.F_TYPE=="2" )
		$("#rdono").attr("checked","true");
	else
		$("#rdoyes").attr("checked","true");	
}

//清除控件空格
function trimkeyup(e) {
    lucene_objInput = $(this);
    if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13) {
        var im = $.trim(lucene_objInput.val());
        lucene_objInput.val(im); 
    }
}   

function userChange(e) {
	getUserInfo();
    lucene_objInput = $(this);    
    var im = $.trim(lucene_objInput.val());
    if(im=="一般用户")
    {
    	var node = $(this).combotree.tree('getRoot');
    	$(this).combotree('setValue',node.id);
    }
    
}   

function showScroll(){
	$('div.datagrid-view2>div.datagrid-body').css('overflow','auto');
}

function hideScroll(e,o){
	if(o.contains(event.toElement ) == false) 
		$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
	
}
//刷新网格
function GirdReload()
{
	$('#grid').datagrid("options").pageNumber=1;
				$('#grid').datagrid({
					url : 'action/master/usermanage/list.json?Token='+ token
//					'queryParams':{
//    				'F_CODE':"",
//    				'F_NAME':"",
//    				'F_ID':""
//				}
				});	
}

//选中网格行
function SelGirdRow(ID)
{
	$('#grid').datagrid("selectRecord",ID);
}

//控制控件只能输入数字
function onlyNum(e) 
{ 
	lucene_objInput = $(this);
	var im = $.trim(lucene_objInput.val());
	
	//第一个if定义除数字外可响应的键,如46对应Delete键,若要响应Tab键,可在后面加上&&!(event.keyCode==9),若要允许输入小数点,则可加上&&!(event.keyCode==190)&&!(event.keyCode==110),其它类似 
	if(!(e.keyCode==46)&&!(e.keyCode==8)&&!(e.keyCode==37)&&!(e.keyCode==39)) 
	{
	//第二个if定义要响应的数字键,||前面的是响应左边键盘对应的数字,后面是响应小键盘上的数字 
	    if(!((e.keyCode>=48&&e.keyCode<=57)||(e.keyCode>=96&&e.keyCode<=105))) 
	    {
	    	return false;
	    }
	}		    	 
} 
//新增
function add()
{
	$("#addForm").attr("action","action/master/usermanage/add.json?Token=" + token+"&rnd="+Math.random());
	var strgroup = "999" ;
	$("#addForm input[name=F_USERGROUPID]").val($("#addUserGroup").combobox("getValue"));
	$("#addForm input[name=F_STATE]").val("0");	

	$.getJSON("action/master/usermanage/getMaxCode.json?Token=" + token+"&rnd="+Math.random(), function(data)
		{
            $("#addForm input[name=F_CODE]").val(data.F_CODE);
            $("#addForm input[name=F_PASSWORD]").val("666666");
            $("#addForm input[name=F_REPASSWORD]").val("666666");
//            $("#addUserGroup").combobox("setValue",strgroup);

            
        });
	openDialog('新增','addDig','grid','addForm',{'height': 400,'width':300,clearForm:true},
		function(){ 
			$("#addDig").dialog('open'); 
			$.getJSON("action/master/usermanage/getMaxCode.json?Token=" + token+"&rnd="+Math.random(), 
			function(data)
		    {
	            $("#addForm input[name=F_CODE]").val(data.F_CODE);
	            $("#addForm input[name=F_PASSWORD]").val("666666");
	            $("#addForm input[name=F_REPASSWORD]").val("666666");
//	            $('#addDept').combotree('setValue',$('#addDept').combotree('getRoot').text);
//	            $("#addUserGroup").combobox("setValue",strgroup);
            });
		}
	);
	var str = "P" ;
	$("#addusertype").combobox('setValue',str);
//	$("#addUserGroup").combobox("setValue",strgroup);
}
//修改
function update(){
	var select = $('#grid').datagrid('getSelected');	
	var row = $('#grid').datagrid('getSelected');
	var vindex = $('#grid').datagrid('getRowIndex', row);//获取选中行索引	
	var ConnStr = "action/master/usermanage/edit.json?Token=" + token;
    if(select){
    	$("#updateForm input[name=F_ID]").val(select.F_ID);
    	$("#updateForm input[name=F_CODE]").val(select.F_CODE);
    	$("#updateForm input[name=F_NAME]").val(select.F_NAME); 
    	$("#txtusertype").combobox('setText',function(){
    			var str="一般用户";    		
    			if(select.F_USERTYPE=="A")
    				{
    					str="超级管理员";
    				}
    			else if(select.F_USERTYPE=="G")
    				{
    					str="管理员";
    				}
    			return str;
    		});
    	$('#txtusergroup').combobox('setText',select.GROUPNAME);
    	var node=$('#edtDept').combotree('tree').tree('find',select.F_DEPTID);
    	var vstr="" ;
    	if(node==null || node=="")
		{
			vstr = select.DEPTNAME ;
		}
    	else{vstr=node.id;}
    	$("#editPHONE").numberbox('setValue',select.F_PHONENO);
    	$("#updateForm").attr("action",ConnStr);
    	openDialog('修改','updateDig','grid','updateForm',{'height':350,'width':300,'RowID':select.F_ID,clearForm:false},
    		//修改成功后，定位到刚才修改的记录
    		function(){
//    			$('#grid').datagrid("options").pageNumber=1;
//				$('#grid').datagrid({
//					url : 'action/master/usermanage/Qrylist.json?Token='+ token,
//					'queryParams':{
//    				'F_ID':select.F_ID
//				}});	
			},
    		function(){},
    		function(){$('#edtDept').combotree('setValue',DeptID);});
    	DeptID = select.F_DEPTID ;
    	DeptName = select.DEPTNAME ;
    	$('#edtDept').combotree('setValue',select.DEPTNAME);
    	//当前登录用户只能修改自己的名称 电话，其他选项不能修改
//		if(select.F_ID==userID)
//		{
//			$("#txtcode").attr("disabled","disabled");
//			$("#txtusertype").combobox("disable");
//			$("#txtusertype").attr("href","javascript:void()");
//			$("#txtusergroup").combotree("disable");
//			$("#edtDept").combotree("disable");			
//		}
//		else
//		{
//			$("#txtcode").removeAttr("disabled");
//			$("#txtusergroup").combotree("enable");
//			$("#edtDept").combotree("enable");	
//			$("#txtusertype").combobox("enable");
//		}
   	}else{
    	parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>请选择一行数据!</p>",timeout:3000,	showType:'slide',width:250,height:150});
   	}
}
//初始化密码
function initPwd(){
    var select = $('#grid').datagrid('getSelected');	
    $("#updateForm input[name=F_ID]").val(select.F_ID);
    $.messager.confirm('提示!','<h5>确定要初始化密码吗?</h5>',function(r){   
    if (r){   
    	    $.getJSON("action/master/usermanage/initPwd.json?F_ID="+select.F_ID+"&Token=" + token+"&rnd="+Math.random(),
            function(data){	$('#grid').datagrid('reload');});  
            parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>密码初始化成功! 初始密码为[666666]!</p>",timeout:3000,showType:'slide',width:250,height:150});
    }   
    }); 
  
}
//查询
function FindData()
{
 query(function(){
				$('#grid').datagrid("options").pageNumber=1;
				$('#grid').datagrid({
					url : 'action/master/usermanage/qrylist.json?Token='+ token,
					'queryParams':{
    				'F_CODE':$("#queryForm input[name=F_CODE]").val(),
    				'F_NAME':$("#queryForm input[name=F_NAME]").val()
				}});
	    		$('#queryDig').dialog('close');	
			},{height:200,width:350},"queryDig","queryForm");
}

//扩展验证
//$.extend($.fn.validatebox.defaults.rules, { 
//    checkEmptyCode:{
//    	validator: function(value, param){
//		    var text = $.trim(value);
//		    return (text.length>0)
//		},
//		message: '编码不能为空'
//    },
//    checkEmptyName:{
//    	validator: function(value, param){
//		    var text =  $.trim(value);
//		    return ((text.length>0)&&(text!=""))
//		},
//		message: '名称不能为空'
//    },
//    checkEmptyPWD:{
//    	validator: function(value, param){
//		    var text =  $.trim(value);
//		    return ((text.length>0)&&(text!=""))
//		},
//		message: '密码不能为空'
//    },
//    checkEmptyREPWD:{
//    	validator: function(value, param){
//		    var text =  $.trim(value);
//		    return ((text.length>0)&&(text!=""))
//		},
//		message: '确认密码不能为空'
//    },
//    Isnum:{
//    	validator: function(value, param){
//		    return /^\d+$/.test(value);
//		},
//		message: '只能输入数字'
//    }
//    
//});
